// pages/historyQuestion/historyQuestion.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    answerList: [],
    timeList: null,
    selectedDate: {},
    number: 0,
    isHistory: 1,
    cheight: 0
  },
  getAnswerList() {
    if (this.data.selectedDate) {
      wx.showLoading()
      console.log(this.data.selectedDate)
      app.requestModule('/app/plan/getHistorySubjectByUnitId', { unitId: this.data.selectedDate.id }, this.getAnswerListCallback, () => { }, 'GET')
    }
    else if (!this.data.selectedDate && this.data.timeList && this.data.timeList.length > 0) {
      wx.showModal({
        title: '提示',
        content: '您还没有选择要回顾的错题哦',
      })
    }
  },
  getAnswerListCallback(res) {
    if (res.data && res.data.length > 0) {
      let answerList = res.data
      let newlist = []
      answerList.forEach((v, k) => {
        let tempobj = {
          title: '',
          selectedId: '',
          isAnswered: true,
          list: []
        }
        tempobj.title = v.itemDesc
        tempobj.selectedId = parseInt(v.answerItem)
        let qlist = [v.choose1, v.choose2, v.choose3, v.choose4, v.choose5, v.choose6]
        qlist = qlist.filter(function (n) { return n });
        qlist.map((text, k) => {
          let answerId = parseInt(v.answerItem)
          let a = {
            id: (k + 1),
            is_true: (k + 1) === parseInt(v.successItem),
            text: text,
            'class': ''
          }
          if (a.is_true) {
            a.class = "right"
          }
          if (!a.is_true && answerId === a.id) {
            a.class = "wrong"
          }
          if (!a.is_true && answerId !== a.id) {
            a.class = "grey"
          }
          tempobj.list.push(a)
        })
        newlist.push(tempobj)
      })
      this.setData({
        answerList: newlist
      })
      wx.hideLoading()
    }
    else {
      wx.hideLoading()
    }
  },
  getTimeList() {
    app.requestModule('/app/plan/getHistoryTimeLine', {}, this.getTimeListCallback, () => { }, 'GET')
  },
  getTimeListCallback(res) {
    if (res.data && res.data.length > 0) {
      let timeList = res.data
      let tlist = timeList.map((v, k) => {
        if (k === timeList.length - 1) v = Object.assign(v, { class: 'active' })
        else v = Object.assign(v, { class: '' })
        return v
      })
      this.setData({
        timeList: tlist,
        selectedDate: tlist[tlist.length - 1],
        scrollId: 'date_' + (tlist.length - 1)
      })
      this.getAnswerList()
    }
  },
  bindtap(e) {
    let dataset = e.currentTarget.dataset
    let item = dataset.item
    let id = dataset.id
    if (item.id !== this.data.selectedDate.id) {
      let timeList = this.data.timeList
      let tlist = timeList.map((v, k) => {
        if (item.id === v.id) v.class = "active"
        else v.class = ""
        return v
      })
      this.setData({
        selectedDate: item,
        timeList: tlist,
        scrollId: 'date_' + id
      })
      this.getAnswerList()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          cheight: res.windowHeight - 44 - 49 - 60
        })
      }
    })
    this.getTimeList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})